<?php

namespace App\Http\Controllers\Api;

use App\Models\Cause;
use App\Models\Course;
use App\Models\User;
use function foo\func;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;
use App\Services\SmsService;
class ScriptController extends Controller
{
    function __construct()
    {
        if(!isset($_GET['key']) || $_GET['key'] != 'fassssdasdsas6y5ssowqcmlgssssssbgfbdgfbfdbdfbdf')
        {
            echo 'error';die;
        }
        ini_set('memory_limit','1024M');
        set_time_limit(600);

    }
    //定时任务执行扣课时晚上11点
    public function CutClass(){
        $html_log = '';
        //查询今天的课程的球队id
        $team_id = Course::getCourse();

        //查询今天球队的成员并且执行扣减课时
        if (empty($team_id)){
            echo 'die';die;
        }
        $user_id = User::whereIn('team_id',$team_id)->pluck('id')->toArray();

        //查询请假的人
        $cause_id = Cause::where('start_date','<',time())->where('end_date','>',time())->pluck('user_id')->toArray();
        //查询休课的uisd
        $lev_uid = User::where('leastatus',1)->pluck('id')->toArray();
        //查询已经过期的和没有课时卡的
        $over_uid = User::where('class_end_time','<',time())->pluck('id')->toArray();
        $heBing = array_merge($cause_id,$lev_uid,$over_uid);

        //找出交集
        $new_user = array_intersect($user_id,$heBing);

        //请假的user_id解决
        $ids = array_diff($user_id,$new_user);

        //对这些id进行扣取课时
        $kou_ids = [];
        foreach ($ids as $k=>$v){
            $kou_ids[]=  $v;
        }

        $res = User::whereIn('id',$kou_ids)->where('period','>',0)->decrement('period');
        $html_log = "\n--------------------------------------------\n"
            .date('Y-m-d H:i:s').'|课时减少|结果|总数：'.json_encode($kou_ids).'|请假：'.json_encode($cause_id).'|过期：'.json_encode($over_uid).'|休课：'.json_encode($lev_uid)."\n"
            .$html_log;
        return $html_log;
    }

    //定时任务发送短信每天晚上8点30
     public function smsNotice(){
         $new_phone = '';
         $html_log = '';
         $start_time = strtotime(date('Y-m-d 0:0:0',time()+86400))-1;

         $end_time  = strtotime(date('Y-m-d 23:59:59',time()+86400))+1;
          //查询明天上课的球队
         $team_id = Course::where('class_end_time','<',$end_time)->where('class_start_time','>',$start_time)->pluck('team_id')->toArray();
         //查询今天球队的成员并且执行扣减课时
         if (empty($team_id)){
             $html_log = "\n--------------------------------------------\n"
                 .date('Y-m-d H:i:s').'|通知短信发送|结果|总数：'.'无'."\n"
                 .$html_log;
             return $html_log;
         }
         $user_id = User::whereIn('team_id',$team_id)->pluck('id')->toArray();
         if (empty($user_id)){
             $html_log = "\n--------------------------------------------\n"
                 .date('Y-m-d H:i:s').'|通知短信发送|结果|总数：'.'无'."\n"
                 .$html_log;
             return $html_log;
         }
         //查询请假的人
         $cause_id = Cause::where('start_date','<',$start_time)->where('end_date','>',$end_time)->pluck('user_id')->toArray();
         //查询休课的uid
         $lev_uid = User::where('leastatus',1)->pluck('id')->toArray();
         //查询已经过期的和没有课时卡的
         $over_uid = User::where('class_end_time','<',time())->pluck('id')->toArray();
         $heBing = array_merge($cause_id,$lev_uid,$over_uid);
         //找出交集
         $new_user = array_intersect($user_id,$heBing);
         //请假的user_id解决(有可能别个队请假的)
         $ids = array_diff($user_id,$new_user);
         //对这些id进行扣取课时
         $kou_ids = [];
         foreach ($ids as $k=>$v){
             $kou_ids[]=  $v;
         }
         //给他发短信
         $user_phone = User::whereIn('id',$kou_ids)->pluck('user_phone')->toArray();
         //拼接手机号
          foreach ($user_phone as $k=>$v){
              $new_phone.= $v.',';
          }
          $new_phone = substr($new_phone, 0, -1);
          $res = SmsService::sendSms($new_phone,Setting()->get('sms_notice_template'),'');
          if ($res){
              $html_log = "\n--------------------------------------------\n"
                  .date('Y-m-d H:i:s').'|通知短信发送|结果|总数：'.$new_phone."\n"
                  .$html_log;
              return $html_log;
          }else{
              $html_log = "\n--------------------------------------------\n"
                  .date('Y-m-d H:i:s').'|通知短信发送|结果|总数：'.'失败'."\n"
                  .$html_log;
              return $html_log;
          }
    }

    //定时任务判断课时卡是否过期
     public function isOver(){
         $html_log = '';
         $over_uid = User::where('level',1)->where('class_end_time','<',time())->pluck('id')->toArray();
         $level_uid = User::where('level',1)->where('period',0)->pluck('id')->toArray();
         $new_uid = array_unique(array_merge($over_uid,$level_uid));
         if (!empty($new_uid)){
             //更改身份
             $res = User::whereIn('id',$new_uid)->update(['level'=>0,'user_level_status'=>1,'period'=>0]);
             $html_log = "\n--------------------------------------------\n"
                 .date('Y-m-d H:i:s').'|课时过期|结果|总数：'.json_encode($over_uid)."\n"
                 .$html_log;
             return $html_log;
         }else{
             $html_log = "\n--------------------------------------------\n"
                 .date('Y-m-d H:i:s').'|课时过期|结果|总数：'.'无'."\n"
                 .$html_log;
             return $html_log;
         }

     }

     //定时任务还剩下7个课时9点发送
    public function overTime(){
        $new_phone = '';
        $html_log = '';
        $over_uid = User::where('class_end_time','>',time())->where('leastatus',0)->where('over_status',0)->where('period',7)->pluck('id')->toArray();
        if (empty($over_uid)) {
            $html_log = "\n--------------------------------------------\n"
                . date('Y-m-d H:i:s') . '|课时过期|结果|总数：' . '无' . "\n"
                . $html_log;
            return $html_log;
        }
        //给他发短信
        $user_phone = User::whereIn('id',$over_uid)->pluck('user_phone')->toArray();
        //拼接手机号
        foreach ($user_phone as $k=>$v){
            $new_phone.= $v.',';
        }
        $new_phone = substr($new_phone, 0, -1);
        //给他发短信
        User::whereIn('id',$over_uid)->update(['over_status'=>1]);
        $res = SmsService::sendSms($new_phone,Setting()->get('sms_over_template'),'');
        if ($res){
            $html_log = "\n--------------------------------------------\n"
                .date('Y-m-d H:i:s').'|通知短信发送|结果|总数：'.$new_phone."\n"
                .$html_log;
            return $html_log;
        }else{
            $html_log = "\n--------------------------------------------\n"
                .date('Y-m-d H:i:s').'|通知短信发送|结果|总数：'.'失败'."\n"
                .$html_log;
            return $html_log;
        }

    }


}
